package com.example.demo.information.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.demo.account.repository.UserRoleRepository;
import com.example.demo.common.utils.Constant;
import com.example.demo.information.entity.Bjxx;
import com.example.demo.information.entity.School;
import com.example.demo.information.entity.Student;
import com.example.demo.information.repository.SchoolRepository;
import com.example.demo.information.repository.StudentRepository;
import com.example.demo.information.service.SchoolService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;

/**
 * @author littlecar
 * @date 2020/3/11 14:18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SchoolServiceImpl implements SchoolService {
    @Autowired
    private SchoolRepository schoolRepository;
    @Autowired
    private UserRoleRepository userRoleRepository;
    @Autowired
    private StudentRepository studentRepository;

    @Override
    public List<School> findByName(String school_name) {
        return schoolRepository.findByName(school_name);
    }

    @Override
    public List<School> findAll() {
        return schoolRepository.findAll();
    }

    @Override
    public School findById(Long school_id) {
        return schoolRepository.findById(school_id).get();
    }

    /**
     * 获得学校map<xxmc,School>
     * @return
     */
    @Override
    public Map<String, School> getSchoolMap() {
        List<School> schools = schoolRepository.findAll();
        Map<String,School> map = new HashMap<>();
        for(School school :schools){
            map.put(school.getName(),school);
        }
        return map;
    }

    /**
     * 获得班级map<school,Map<bjmc,Bjxx>>
     * @return
     */
    @Override
    public Map<Long, Map<String, Bjxx>> getXXBjxxMap() {
        List<School> schools = schoolRepository.findAll();
        Map<Long,Map<String,Bjxx>> map = new HashMap<>();
        for(School school :schools){
            Map<String,Bjxx> map2 = new HashMap<>();
            Set<Bjxx> bjxxList = school.getBjxxList();
            for(Bjxx bjxx:bjxxList){
                map2.put(bjxx.getName(),bjxx);
            }
            map.put(school.getId(),map2);
        }
        return map;
    }

    @Override
    public void save(School school) {
        schoolRepository.save(school);
    }

    @Override
    public List<School> findPageAll(int pages, Integer limit) {
        return schoolRepository.findPageAll(pages,limit);
    }

    @Override
    public List<School> findPageByNameLike(String school_name, int pages, Integer limit) {
        return schoolRepository.findPageByNameLike(school_name,  pages,  limit) ;
    }
    @Override
    public List<School> findByNameLike(String school_name) {
        return schoolRepository.findByNameLike(school_name) ;
    }

    @Override
    public void deleteById(Long school_id) {
        schoolRepository.deleteById(school_id);
        userRoleRepository.deleteBySchoolId(school_id);
    }

    @Override
    public int countAll() {
        return (int) schoolRepository.count();
    }

    @Override
    public int countByNameLike(String school_name) {
        return schoolRepository.countByNameLike(school_name);
    }

    /**
     * 获得索引树（学校-班级）
     * @return
     */
    @Override
    public Map<String,Object> getXxxxBjxxTree() {
        List<School> schools = schoolRepository.findAll();
        Map<String,Object> treeMap = new LinkedHashMap<>();
        for(School s : schools){
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("title",s.getName());
            map.put("id", Constant.TreeTypeSchool+"@@"+s.getId());
            Set<Bjxx> bjxxList = s.getBjxxList();
            boolean xxFlag = true;
            if(bjxxList!=null&&!bjxxList.isEmpty()){
                List<Map<String,Object>> childTreeMap = new ArrayList<>();
                for(Bjxx b:bjxxList){
                    Map<String,Object> childMap = new LinkedHashMap<>();
                    childMap.put("title",b.getName());
                    childMap.put("id",Constant.TreeTypeBjxx+"@@"+b.getId());
                    //TODO 如果bjxx里内有StuList的话，这里可以替换掉
                    List<Student> stuList = studentRepository.findByBjxxId(b.getId());
                    boolean bjDisabledFlag = stuList!=null&&!stuList.isEmpty()?false:true;
                    xxFlag = !bjDisabledFlag?false:xxFlag;
                    childMap.put("disabled",bjDisabledFlag);
                    childTreeMap.add(childMap);
                }
                map.put("children",childTreeMap);
            }
            map.put("disabled",xxFlag);
            treeMap.put(map.get("id")+"@@",map);
        }
        System.out.println(JSON.toJSONString(treeMap));
        return treeMap;
    }
}
